package ihm;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.Timer;

import data.Alerte;
import data.Authentification;
import data.Chat;
import data.Chrono;
import data.Indice;
import data.Log;
import data.ModeleChat;
import data.ModeleChronoChecker;
import data.ModeleDynamiqueObjet;
import data.ModeleDynamiqueObjetAlerte;
import data.ModeleDynamiqueObjetIndice;
import data.ModeleLog;
import data.Temps;
import data.TimerListener;
import data.Utilisateur;
import database.ConnexionDB;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;

import java.awt.Desktop;
import java.awt.Font;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SpringLayout;
import java.awt.Dimension;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;

import network.Connexion;
import network.FileSynchro;
import network.ModeleConnexion;
import network.Protocol;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;


public class FenetrePrincipale extends JFrame{
	
	private JPanel contentPane;
	private JTextField txtEnvoyerChat;
	private JTabbedPane tpnOnglets;
	private Timer timer;
	private JLabel lblChrono;
	
	/** Utilisateurs **/
	private ModeleDynamiqueObjet modele;
    private JTable tabUtilisateurs;
    
    /** Alertes **/
	private ModeleDynamiqueObjetAlerte modele_alertes;
    private JTable tableau_alertes;
    
    /** Indices **/
	private ModeleDynamiqueObjetIndice modele_indices;
    private JTable tabIndices;
    private JTextArea txtaAffichageChat;
	
    /** Chat **/
    private ModeleChat mc;
    private Chat chat = new Chat();
    
    /** Log **/
    private ModeleLog mlog;
    private Log log = new Log();
    
    /** Authentification **/
    private Authentification auth = new Authentification();
    
    /** Chrono **/
    private Chrono chronopartie = new Chrono(new Temps(0,0,0));
    
    
    /** Protocol **/    
	final Protocol p = new Protocol(auth, chat, log);
	
	/** Socket */
	private Connexion connexion = new Connexion(p);
	
	/** Dropbox */
	private FileSynchro dropbox;
    
	
	/**
	 * Create the frame.
	 */
	public FenetrePrincipale(ModeleDynamiqueObjet mdu,ModeleDynamiqueObjetIndice mdi,ModeleDynamiqueObjetAlerte mda) {
		setTitle("Murder Party - Maitre du jeu");
		setBounds(100, 100, 1000, 650);
		this.addWindowListener(new WindowAdapter() {
	        public void windowClosing(WindowEvent evt) {
	        	try {
					modele.getDB().arretDB();
				} catch (SQLException e) {
					System.err.println("Closing DB error");
				}
	        	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        } 
		} );
		
		modele = mdu;
		modele_alertes = mda; 
		modele_indices = mdi;
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnAction = new JMenu("Connexion");
		menuBar.add(mnAction);
		
		final JMenuItem mntmSeConnecter = new JMenuItem("Se connecter");
		mntmSeConnecter.setEnabled(true);
		final JMenuItem mntmSeDconnecter = new JMenuItem("Se d\u00E9connecter");
		mntmSeDconnecter.setEnabled(false);
		
		mnAction.add(mntmSeConnecter);
		mnAction.add(mntmSeDconnecter);
		
		JMenu mnJeu = new JMenu("Partie");
		mnJeu.setEnabled(true);
		menuBar.add(mnJeu);
		
		JMenuItem mntmRgles = new JMenuItem("Modifier le temps de jeu...");
		mnJeu.add(mntmRgles);
		mntmRgles.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				FenetreRegle fr = new FenetreRegle(chronopartie);
				fr.setVisible(true);
			}
		});
		
		
		final JMenuItem mntmStartStop = new JMenuItem("Bloquer/D\u00E9bloquer");
		mntmStartStop.setEnabled(true);
		mnJeu.add(mntmStartStop);
		mntmStartStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(chronopartie.is_pause()){
					chronopartie.start();
					 mntmStartStop.setText("Bloquer");
				}					
				else {
					chronopartie.stop();
					mntmStartStop.setText("D\u00E9bloquer");
				}
			}
		});
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		
		JPanel pnlInfosChrono = new JPanel();
		sl_contentPane.putConstraint(SpringLayout.SOUTH, pnlInfosChrono, 150, SpringLayout.NORTH, contentPane);
		pnlInfosChrono.setMaximumSize(new Dimension(10, 10));
		sl_contentPane.putConstraint(SpringLayout.NORTH, pnlInfosChrono, 0, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, pnlInfosChrono, 0, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, pnlInfosChrono, 0, SpringLayout.EAST, contentPane);
		pnlInfosChrono.setBackground(UIManager.getColor("Button.background"));
		contentPane.add(pnlInfosChrono);
		
		JPanel pnlOnglets = new JPanel();
		pnlOnglets.setBorder(new TitledBorder(null, "Partie", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		sl_contentPane.putConstraint(SpringLayout.NORTH, pnlOnglets, 0, SpringLayout.SOUTH, pnlInfosChrono);
		sl_contentPane.putConstraint(SpringLayout.WEST, pnlOnglets, 0, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, pnlOnglets, 0, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, pnlOnglets, -465, SpringLayout.EAST, contentPane);
		pnlOnglets.setBackground(UIManager.getColor("Button.background"));
		contentPane.add(pnlOnglets);
		
		JPanel pnlChat = new JPanel();
		pnlChat.setBorder(new TitledBorder(null, "Chat", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		sl_contentPane.putConstraint(SpringLayout.NORTH, pnlChat, 0, SpringLayout.SOUTH, pnlInfosChrono);
		SpringLayout sl_pnlInfosChrono = new SpringLayout();
		pnlInfosChrono.setLayout(sl_pnlInfosChrono);
		
		JPanel pnlInfosLog = new JPanel();
		sl_pnlInfosChrono.putConstraint(SpringLayout.NORTH, pnlInfosLog, 0, SpringLayout.NORTH, pnlInfosChrono);
		sl_pnlInfosChrono.putConstraint(SpringLayout.SOUTH, pnlInfosLog, 0, SpringLayout.SOUTH, pnlInfosChrono);
		sl_pnlInfosChrono.putConstraint(SpringLayout.EAST, pnlInfosLog, -465, SpringLayout.EAST, pnlInfosChrono);
		pnlInfosLog.setBorder(new TitledBorder(null, "Historique", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		sl_pnlInfosChrono.putConstraint(SpringLayout.WEST, pnlInfosLog, 0, SpringLayout.WEST, pnlInfosChrono);
		pnlInfosChrono.add(pnlInfosLog);
		
		lblChrono = new JLabel("00 : 00");
		sl_pnlInfosChrono.putConstraint(SpringLayout.NORTH, lblChrono, 0, SpringLayout.NORTH, pnlInfosChrono);
		sl_pnlInfosChrono.putConstraint(SpringLayout.WEST, lblChrono, 0, SpringLayout.EAST, pnlInfosLog);
		sl_pnlInfosChrono.putConstraint(SpringLayout.SOUTH, lblChrono, 0, SpringLayout.SOUTH, pnlInfosChrono);
		sl_pnlInfosChrono.putConstraint(SpringLayout.EAST, lblChrono, 0, SpringLayout.EAST, pnlInfosChrono);
		lblChrono.setHorizontalAlignment(SwingConstants.CENTER);
		SpringLayout sl_pnlInfosLog = new SpringLayout();
		pnlInfosLog.setLayout(sl_pnlInfosLog);
		
		JScrollPane sclpnInfosLog = new JScrollPane();
		sl_pnlInfosLog.putConstraint(SpringLayout.NORTH, sclpnInfosLog, 0, SpringLayout.NORTH, pnlInfosLog);
		sl_pnlInfosLog.putConstraint(SpringLayout.WEST, sclpnInfosLog, 0, SpringLayout.WEST, pnlInfosLog);
		sl_pnlInfosLog.putConstraint(SpringLayout.SOUTH, sclpnInfosLog, 0, SpringLayout.SOUTH, pnlInfosLog);
		sl_pnlInfosLog.putConstraint(SpringLayout.EAST, sclpnInfosLog, 0, SpringLayout.EAST, pnlInfosLog);
		pnlInfosLog.add(sclpnInfosLog);
		
		JTextArea txtaInfosLog = new JTextArea();
		txtaInfosLog.setEditable(false);
		txtaInfosLog.setLineWrap(true);
		sclpnInfosLog.setViewportView(txtaInfosLog);
		lblChrono.setFont(new Font("Microsoft Sans Serif", Font.PLAIN, 65));
		pnlInfosChrono.add(lblChrono);
		sl_contentPane.putConstraint(SpringLayout.WEST, pnlChat, 0, SpringLayout.EAST, pnlOnglets);
		SpringLayout sl_pnlOnglets = new SpringLayout();
		pnlOnglets.setLayout(sl_pnlOnglets);
		
		tpnOnglets = new JTabbedPane(JTabbedPane.TOP);

		/*Onglet1 utilisateur*/
		tabUtilisateurs = new JTable(modele);
		tabUtilisateurs.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				afficherPopup(e);
			}
			@Override
			public void mouseReleased(MouseEvent arg0) {
				int selectedRow = tabUtilisateurs.rowAtPoint(arg0.getPoint());
				if (selectedRow != -1)
				{
					tabUtilisateurs.changeSelection(selectedRow, -1, false, false);
					afficherPopup(arg0);
				}
			
			}
		});
	    modele.getDB().addObserver(modele);   
	    
        /* TODO plus propre */
        modele.update(modele.getDB(), null);
        JPanel ongUtilisateurs  = new JPanel();
        SpringLayout sl_ongUtilisateurs = new SpringLayout();
        ongUtilisateurs.setLayout(sl_ongUtilisateurs);
        
        JScrollPane sclpnUtilisateurs = new JScrollPane(tabUtilisateurs);
        sl_ongUtilisateurs.putConstraint(SpringLayout.NORTH, sclpnUtilisateurs, 0, SpringLayout.NORTH, ongUtilisateurs);
        sl_ongUtilisateurs.putConstraint(SpringLayout.WEST, sclpnUtilisateurs, 0, SpringLayout.WEST, ongUtilisateurs);
        sl_ongUtilisateurs.putConstraint(SpringLayout.SOUTH, sclpnUtilisateurs, -37, SpringLayout.SOUTH, ongUtilisateurs);
        sl_ongUtilisateurs.putConstraint(SpringLayout.EAST, sclpnUtilisateurs, 0, SpringLayout.EAST, ongUtilisateurs);
        ongUtilisateurs.add(sclpnUtilisateurs);
        
        JButton btnAjouterUtilisateur = new JButton("Ajouter");
        btnAjouterUtilisateur.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		FenetreUtilisateurAjouter fua = null;
				fua = new FenetreUtilisateurAjouter(null,"Ajouter un utilisateur","Ajouter",modele,log);	
				fua.setVisible(true);
        	}
        });
        sl_ongUtilisateurs.putConstraint(SpringLayout.NORTH, btnAjouterUtilisateur, 5, SpringLayout.SOUTH, sclpnUtilisateurs);
        sl_ongUtilisateurs.putConstraint(SpringLayout.WEST, btnAjouterUtilisateur, 10, SpringLayout.WEST, ongUtilisateurs);
        ongUtilisateurs.add(btnAjouterUtilisateur);
        
        JButton btnSupprimerUtilisateur = new JButton("Supprimer");
        btnSupprimerUtilisateur.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        			deleteUtilisateurs();
        	}
        });
        sl_ongUtilisateurs.putConstraint(SpringLayout.WEST, btnSupprimerUtilisateur, 6, SpringLayout.EAST, btnAjouterUtilisateur);
        sl_ongUtilisateurs.putConstraint(SpringLayout.SOUTH, btnSupprimerUtilisateur, 0, SpringLayout.SOUTH, btnAjouterUtilisateur);
        ongUtilisateurs.add(btnSupprimerUtilisateur);
        tpnOnglets.addTab("Utilisateurs",ongUtilisateurs);
        
        JButton btnModifierUtilisateur = new JButton("Modifier");
        btnModifierUtilisateur.addActionListener(new ActionListener() {
	        public void actionPerformed(ActionEvent arg0) {
	        	if(tabUtilisateurs.getSelectedRow()!= -1)
	        	{
					Utilisateur u = modele.getListUtilisateur().get(tabUtilisateurs.getSelectedRow());
					FenetreUtilisateurAjouter fau = null;
					fau = new FenetreUtilisateurAjouter(u,"Modifier un utilisateur","Modifier",modele,log);	
					fau.setVisible(true);
				}
	        	else
	        	{
	        		JOptionPane.showMessageDialog(null, "Vous devez s�l�ctionner une ligne.", "Modifier", JOptionPane.INFORMATION_MESSAGE);
	        	}
			}
        });    
        sl_ongUtilisateurs.putConstraint(SpringLayout.NORTH, btnModifierUtilisateur, 6, SpringLayout.SOUTH, sclpnUtilisateurs);
        sl_ongUtilisateurs.putConstraint(SpringLayout.WEST, btnModifierUtilisateur, 6, SpringLayout.EAST, btnSupprimerUtilisateur);
        ongUtilisateurs.add(btnModifierUtilisateur);
        /*--------------------*/
        
        
        /*Onglet2 alertes*/
        
		tableau_alertes = new JTable(modele_alertes);
		tableau_alertes.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				afficherPopupAlerte(e);
			}
			@Override
			public void mouseReleased(MouseEvent arg0) {
				int selectedRow = tableau_alertes.rowAtPoint(arg0.getPoint());
				if (selectedRow != -1)
				{
					tableau_alertes.changeSelection(selectedRow, -1, false, false);
					afficherPopupAlerte(arg0);
				}
			
			}
		});
		modele_alertes.getDB().addObserver(modele_alertes); 
		
        /* TODO plus propre */
		modele_alertes.update(modele_alertes.getDB(), null);
		
        JPanel ongAlertes  = new JPanel();
        
        SpringLayout sl_ongAlertes = new SpringLayout();
        ongAlertes.setLayout(sl_ongAlertes);
        
        JScrollPane sclpnAlertes = new JScrollPane(tableau_alertes);
        sl_ongAlertes.putConstraint(SpringLayout.NORTH, sclpnAlertes, 0, SpringLayout.NORTH, ongAlertes);
        sl_ongAlertes.putConstraint(SpringLayout.WEST, sclpnAlertes, 0, SpringLayout.WEST, ongAlertes);
        sl_ongAlertes.putConstraint(SpringLayout.SOUTH, sclpnAlertes, -37, SpringLayout.SOUTH, ongAlertes);
        sl_ongAlertes.putConstraint(SpringLayout.EAST, sclpnAlertes, 0, SpringLayout.EAST, ongAlertes);
        ongAlertes.add(sclpnAlertes);
        
        JButton btnAjouterAlerte = new JButton("Ajouter");
        btnAjouterAlerte.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
            		FenetreAlerteAjouter faa = null;
    				faa = new FenetreAlerteAjouter(null,"Ajouter une alerte","Ajouter",modele_alertes, modele);	
    				faa.setVisible(true);
        	}
        });
        sl_ongAlertes.putConstraint(SpringLayout.NORTH, btnAjouterAlerte, 5, SpringLayout.SOUTH, sclpnAlertes);
        sl_ongAlertes.putConstraint(SpringLayout.WEST, btnAjouterAlerte, 10, SpringLayout.WEST, ongAlertes);
        ongAlertes.add(btnAjouterAlerte);
        
        JButton btnSupprimerAlerte = new JButton("Supprimer");
        btnSupprimerAlerte.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				deleteAlertes();
        	}
        });
        sl_ongAlertes.putConstraint(SpringLayout.WEST,  btnSupprimerAlerte, 6, SpringLayout.EAST, btnAjouterAlerte);
        sl_ongAlertes.putConstraint(SpringLayout.SOUTH,  btnSupprimerAlerte, 0, SpringLayout.SOUTH, btnAjouterAlerte);
        ongAlertes.add(btnSupprimerAlerte);
        
        JButton btnModifierAlerte = new JButton("Modifier");
        btnModifierAlerte.addActionListener(new ActionListener() {
	        public void actionPerformed(ActionEvent arg0) {
	        	if(tableau_alertes.getSelectedRow()!= -1)
	        	{
					Alerte a = modele_alertes.getlistAlertes().get(tableau_alertes.getSelectedRow());
					FenetreAlerteAjouter faa = null;
					faa = new FenetreAlerteAjouter(a,"Modifier une alerte","Modifier",modele_alertes,modele);	
					faa.setVisible(true);
				}
	        	else
	        	{
	        		JOptionPane.showMessageDialog(null, "Vous devez s\u00E9lectionner une ligne.", "Modifier", JOptionPane.INFORMATION_MESSAGE);
	        	}
			}
        });    
	       
        sl_ongAlertes.putConstraint(SpringLayout.NORTH, btnModifierAlerte, 6, SpringLayout.SOUTH, sclpnAlertes);
        sl_ongAlertes.putConstraint(SpringLayout.WEST, btnModifierAlerte, 6, SpringLayout.EAST, btnSupprimerAlerte);
        ongAlertes.add(btnModifierAlerte);
        
        tpnOnglets.addTab("Alertes",ongAlertes);
        /*-----------------------*/
        
        
        /*Onglet3 indices*/
		tabIndices = new JTable(modele_indices);
		tabIndices.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				afficherPopup(e);
			}
			@Override
			public void mouseReleased(MouseEvent arg0) {
				int selectedRow = tabIndices.rowAtPoint(arg0.getPoint());
				if (selectedRow != -1)
				{
					tabIndices.changeSelection(selectedRow, -1, false, false);
					afficherPopup(arg0);
				}
			
			}
		});
		modele_indices.getDB().addObserver(modele_indices);   
        /* TODO plus propre */
		modele_indices.update(modele_indices.getDB(), null);
        JPanel ongIndices  = new JPanel();
        SpringLayout sl_ongIndices = new SpringLayout();
        ongIndices.setLayout(sl_ongIndices);
        
        JScrollPane sclpnIndices = new JScrollPane(tabIndices);
        sl_ongIndices.putConstraint(SpringLayout.NORTH, sclpnIndices, 0, SpringLayout.NORTH, ongIndices);
        sl_ongIndices.putConstraint(SpringLayout.WEST, sclpnIndices, 0, SpringLayout.WEST, ongIndices);
        sl_ongIndices.putConstraint(SpringLayout.SOUTH, sclpnIndices, -37, SpringLayout.SOUTH, ongIndices);
        sl_ongIndices.putConstraint(SpringLayout.EAST, sclpnIndices, 0, SpringLayout.EAST, ongIndices);
        ongIndices.add(sclpnIndices);
        
        JButton btnAjouterIndice = new JButton("Ajouter");
        btnAjouterIndice.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		FenetreIndiceAjouter f1 = null;
				try {
					f1 = new FenetreIndiceAjouter(modele_indices,log);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				f1.setVisible(true);
        	}
        });
        sl_ongIndices.putConstraint(SpringLayout.NORTH, btnAjouterIndice, 5, SpringLayout.SOUTH, sclpnIndices);
        sl_ongIndices.putConstraint(SpringLayout.WEST, btnAjouterIndice, 10, SpringLayout.WEST, ongIndices);
        ongIndices.add(btnAjouterIndice);
        
        JButton btnSupprimerIndice = new JButton("Supprimer");
        btnSupprimerIndice.addActionListener(new ActionListener() {
           	public void actionPerformed(ActionEvent arg0) {
    			deleteIndices();
    			auth.setUtilisateur(auth.getUtilisateur());
    	}
        });
        sl_ongIndices.putConstraint(SpringLayout.WEST,  btnSupprimerIndice, 6, SpringLayout.EAST, btnAjouterIndice);
        sl_ongIndices.putConstraint(SpringLayout.SOUTH,  btnSupprimerIndice, 0, SpringLayout.SOUTH, btnAjouterIndice);
        ongIndices.add(btnSupprimerIndice);
        
        JButton btnModifierIndice = new JButton("Modifier");
        btnModifierIndice.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent arg0) {
    		FenetreModifierIndice f1 = null;
    		
    		int id_indice;
    		id_indice = getIdIndice();
    		
    		if (id_indice != -1)
    		{
			try {
				f1 = new FenetreModifierIndice(modele_indices,id_indice, auth,log);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			f1.setVisible(true);
    	}
    		//f1.dispose();
    	}
        });
    	
    	
        sl_ongIndices.putConstraint(SpringLayout.NORTH, btnModifierIndice, 6, SpringLayout.SOUTH, sclpnIndices);
        sl_ongIndices.putConstraint(SpringLayout.WEST, btnModifierIndice, 6, SpringLayout.EAST, btnSupprimerIndice);
        ongIndices.add(btnModifierIndice);
      
        JButton btnvoirIndice = new JButton("Voir");
        btnvoirIndice.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent arg0) {
    	
    		int id_indice = getIdIndice();
    		final Indice ind1 = modele_indices.getInfosIndice(id_indice);
    		
    		File fichier_indice = new File("data\\"+ind1.getCheminIndice() );
			boolean test =OuvrirFile(fichier_indice);
			if ( test == true )
			{
				System.out.println("test = true");
			}
			else
			{
				System.out.println("test = false");
			}
		}
        });
        
      
        
        
        JButton btnEnvoyerIndice = new JButton("Envoyer un indice");
        btnEnvoyerIndice.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent arg0) {
    		FenetreEnvoyerIndice f1 = null;
    		
    	
			try {
				f1 = new FenetreEnvoyerIndice(modele_indices, auth);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			f1.setVisible(true);
    	}
        });
    	
    	
        sl_ongIndices.putConstraint(SpringLayout.NORTH, btnEnvoyerIndice, 6, SpringLayout.SOUTH, sclpnIndices);
        sl_ongIndices.putConstraint(SpringLayout.WEST, btnEnvoyerIndice, 6, SpringLayout.EAST, btnModifierIndice);
        ongIndices.add(btnEnvoyerIndice);
        
        sl_ongIndices.putConstraint(SpringLayout.NORTH, btnvoirIndice, 6, SpringLayout.SOUTH, sclpnIndices);
        sl_ongIndices.putConstraint(SpringLayout.WEST, btnvoirIndice, 6, SpringLayout.EAST, btnEnvoyerIndice);
        ongIndices.add(btnvoirIndice);
        
        
        tpnOnglets.addTab("Indices",ongIndices);
        /*-----------------------*/
		
		sl_pnlOnglets.putConstraint(SpringLayout.NORTH, tpnOnglets, 0, SpringLayout.NORTH, pnlOnglets);
		sl_pnlOnglets.putConstraint(SpringLayout.WEST, tpnOnglets, 0, SpringLayout.WEST, pnlOnglets);
		sl_pnlOnglets.putConstraint(SpringLayout.SOUTH, tpnOnglets, 0, SpringLayout.SOUTH, pnlOnglets);
		sl_pnlOnglets.putConstraint(SpringLayout.EAST, tpnOnglets, 0, SpringLayout.EAST, pnlOnglets);
		pnlOnglets.add(tpnOnglets);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, pnlChat, 0, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, pnlChat, 0, SpringLayout.EAST, contentPane);
		pnlChat.setBackground(UIManager.getColor("Button.background"));
		contentPane.add(pnlChat);
		SpringLayout sl_pnlChat = new SpringLayout();
		pnlChat.setLayout(sl_pnlChat);
		
		txtEnvoyerChat = new JTextField();
		sl_pnlChat.putConstraint(SpringLayout.WEST, txtEnvoyerChat, 0, SpringLayout.WEST, pnlChat);
		sl_pnlChat.putConstraint(SpringLayout.SOUTH, txtEnvoyerChat, 0, SpringLayout.SOUTH, pnlChat);
		sl_pnlChat.putConstraint(SpringLayout.EAST, txtEnvoyerChat, 345, SpringLayout.WEST, pnlChat);
		pnlChat.add(txtEnvoyerChat);
		txtEnvoyerChat.setColumns(10);
		
		JButton btnEnvoyerChat = new JButton("Envoyer");
		sl_pnlChat.putConstraint(SpringLayout.WEST, btnEnvoyerChat, 6, SpringLayout.EAST, txtEnvoyerChat);
		sl_pnlChat.putConstraint(SpringLayout.SOUTH, btnEnvoyerChat, 0, SpringLayout.SOUTH, pnlChat);
		btnEnvoyerChat.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		if(connexion.getState()){
        			connexion.send(txtEnvoyerChat.getText());
        			chat.addLine("[Admin] " + txtEnvoyerChat.getText());
        		}
        		else {
					log.addLine("[-] Impossible d'envoyer un message sans �tre connect\u00E9");
        		}
        	}
        });
		pnlChat.add(btnEnvoyerChat);
		
		JScrollPane sclpnAffichageChat = new JScrollPane();
		sl_pnlChat.putConstraint(SpringLayout.NORTH, sclpnAffichageChat, 20, SpringLayout.NORTH, pnlChat);
		sl_pnlChat.putConstraint(SpringLayout.WEST, sclpnAffichageChat, 0, SpringLayout.WEST, pnlChat);
		sl_pnlChat.putConstraint(SpringLayout.SOUTH, sclpnAffichageChat, -10, SpringLayout.NORTH, btnEnvoyerChat);
		sl_pnlChat.putConstraint(SpringLayout.EAST, sclpnAffichageChat, 0, SpringLayout.EAST, pnlChat);
		pnlChat.add(sclpnAffichageChat);
		
		txtaAffichageChat = new JTextArea();
		txtaAffichageChat.setLineWrap(true);
		sclpnAffichageChat.setViewportView(txtaAffichageChat);
		
		/** Observer **/
		mlog = new ModeleLog(txtaInfosLog);
		log.addObserver(mlog);
		
		mc = new ModeleChat(txtaAffichageChat);
		chat.addObserver(mc);
		
		TimerListener timerListener;
		timer = new Timer(1000, timerListener = new TimerListener(chronopartie,1,19,30));
		startTimerGeneral();
		modele.getDB().addObserver(timerListener);
		
		
		
		/** Bouton connexion / deconnexion */
		/** TODO les logs doivent etre gerer par le modele
		 **/
		

		ModeleConnexion mcon = new ModeleConnexion(auth,modele.getDB(), chronopartie,lblChrono, mntmSeConnecter,mntmSeDconnecter,modele_alertes ,log);

		connexion.addObserver(mcon);
		
		mntmSeDconnecter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				log.addLine("[+] D\u00E9connexion ...");
				connexion.disconnect();
			}
		});
		
		mntmSeConnecter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				FenetreAuthentifier f = new FenetreAuthentifier(connexion, log);
				f.setAlwaysOnTop(true);
				f.setVisible(true);
			}
		});
		
		
	}
	
	protected int getIdIndice() {

		
		int result =0 ;
	
			int ligneSelectionner[] = tabIndices.getSelectedRows();
			List<Integer> idModif = new ArrayList<Integer>();
				if(ligneSelectionner.length == 0)
				{
					JOptionPane.showMessageDialog(null,"Veuillez s\u00E9lectionner un indice");
					result = -1 ;
				}
				else if (ligneSelectionner.length >1)
				{
					JOptionPane.showMessageDialog(null,"Vous ne pouvez pas s\u00E9lectionner plus d'un indice");
					result = -1;
				}
				
				else 
				{
					//Enregistre tous les id des utilisateurs a supprimer dans une liste
					for(int i=0; i<ligneSelectionner.length;i++)
					{
						idModif.add(modele_indices.getListIndice().get(ligneSelectionner[i]).getIdIndice());
					}
					//Supprime tous les utilisateur de la liste avec leur id
					for(int i=0; i<idModif.size();i++)
					{
						result = idModif.get(i);
					
						
						//log.addLine("[+] Supression utilisateur:" + idSupprimer.get(i));
						
						/*class ActionListenerChrono implements ActionListener {

	JTextField txtchrono;
	Chrono c;
	public ActionListenerChrono(JTextField txtchrono, Chrono c){
		this.txtchrono = txtchrono;
		this.c = c;
	}
	
	public void actionPerformed(ActionEvent e) {
		String s = txtchrono.getText();
		long l = Long.decode(s);
		if(l > 0){
			c.setChrono(new Temps(l));
		}
	}
	
} C quoi ça !? */
						//Thread.sleep(30);
					}	
					
				}
				
				return result;
		}
	

	public void deleteUtilisateurs()
	{
		try {
			int ligneSelectionner[] = tabUtilisateurs.getSelectedRows();
			List<Integer> idSupprimer = new ArrayList<Integer>();
				if(ligneSelectionner.length != 0)
				{
					//Enregistre tous les id des utilisateurs a supprimer dans une liste
					for(int i=0; i<ligneSelectionner.length;i++)
					{
						idSupprimer.add(modele.getListUtilisateur().get(ligneSelectionner[i]).getIdUtilisateur());
					}
					//Supprime tous les utilisateur de la liste avec leur id
					for(int i=0; i<idSupprimer.size();i++)
					{
						Thread t = modele.deleteUtilisateur(idSupprimer.get(i));
						log.addLine("[+] Supression utilisateur:" + idSupprimer.get(i));
						
						t.join();
					}	
					
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e){
			e.printStackTrace();
		}
	}
	
	public void deleteIndices()
	{
		try {
			int ligneSelectionner[] = tabIndices.getSelectedRows();
			List<Integer> idSupprimer = new ArrayList<Integer>();
			
			if(ligneSelectionner.length == 0)
			{
				JOptionPane.showMessageDialog(null,"Veuillez s\u00E9lectionner un indice");
			
			}
			
			
			
			
			else
				{
					//Enregistre tous les id des indices a supprimer dans une liste
					for(int i=0; i<ligneSelectionner.length;i++)
					{
						idSupprimer.add(modele_indices.getListIndices().get(ligneSelectionner[i]).getIdIndice());
					}
					//Supprime tous les utilisateur de la liste avec leur id
					for(int i=0; i<idSupprimer.size();i++)
					{
						File f1 = new File("data\\"+modele_indices.getChemin(idSupprimer.get(i)));
						f1.delete();
						modele_indices.deleteIndice(idSupprimer.get(i));
						log.addLine("[+] Supression indices:" + idSupprimer.get(i));
						
						/** ?? */
						Thread.sleep(30);
					}	
					
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e){
			e.printStackTrace();
		}
	}
	
	
	
	public void deleteAlertes()
	{
		try {
			int ligneSelectionner[] = tableau_alertes.getSelectedRows();
			List<Integer> idSupprimer = new ArrayList<Integer>();
				if(ligneSelectionner.length != 0)
				{
					for(int i=0; i<ligneSelectionner.length;i++)
					{
						idSupprimer.add(modele_alertes.getlistAlertes().get(ligneSelectionner[i]).getIdAlerte());
					}
					for(int i=0; i<idSupprimer.size();i++)
					{
						modele_alertes.getDB().deleteAlertesUtilisateur(idSupprimer.get(i));
						Thread t = modele_alertes.deleteAlerte(idSupprimer.get(i));
						log.addLine("[+] Supression alertes:" + idSupprimer.get(i));
						
						t.join();
					}
					
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	private void afficherPopup(MouseEvent e)
	{
        if (e.isPopupTrigger()) {
        	JPopupMenu contextMenu = new JPopupMenu();
            JMenuItem ajouter = new JMenuItem("Ajouter");
            ajouter.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				FenetreUtilisateurAjouter fua = null;
    				fua = new FenetreUtilisateurAjouter(null,"Ajouter un utilisateur","Ajouter",modele,log);	
    				fua.setVisible(true);
    			}
    		});
            JMenuItem supprimer = new JMenuItem("Supprimer"); 
            supprimer.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				deleteUtilisateurs();
    			}
    		});
            
            contextMenu.add(ajouter);
            contextMenu.add(supprimer);
            contextMenu.setEnabled(true);
            contextMenu.setVisible(true); 
            getContentPane().add(contextMenu);
            contextMenu.show(tabUtilisateurs, e.getX(), e.getY());
         }
	}
	
	private void afficherPopupAlerte(MouseEvent e)
	{
        if (e.isPopupTrigger()) {
        	JPopupMenu contextMenu = new JPopupMenu();
            JMenuItem ajouter = new JMenuItem("Ajouter");
            ajouter.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				FenetreAlerteAjouter faa = null;
    				faa = new FenetreAlerteAjouter(null,"Ajouter une alerte","Ajouter",modele_alertes,modele);	
    				faa.setVisible(true);
    			}
    		});
            JMenuItem modifier = new JMenuItem("Modifier");
            modifier.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				Alerte a = modele_alertes.getlistAlertes().get(tableau_alertes.getSelectedRow());
    				FenetreAlerteAjouter faa = null;
    				faa = new FenetreAlerteAjouter(a,"Modifier une alerte","Modifier",modele_alertes,modele);	
    				faa.setVisible(true);
    			}
    		});
            JMenuItem supprimer = new JMenuItem("Supprimer"); 
            supprimer.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				deleteAlertes();
    			}
    		});
            
            contextMenu.add(modifier);
            contextMenu.add(ajouter);
            contextMenu.add(supprimer);
            contextMenu.setEnabled(true);
            contextMenu.setVisible(true); 
            getContentPane().add(contextMenu);
            contextMenu.show(tableau_alertes, e.getX(), e.getY());
         }
	}

public boolean OuvrirFile(final File file) {
	  if (!Desktop.isDesktopSupported()) {
	    return false;
	  }

	  Desktop desktop = Desktop.getDesktop();
	  if (!desktop.isSupported(Desktop.Action.OPEN)) {
	    return false;
	  }

	  try {
	    desktop.open(file);
	  } catch (IOException e) {
	    // Log an error
	    return false;
	  }

	  return true;
	}
	public void stopTimerGeneral()
	{
	    timer.stop();	
	}
	
	public void startTimerGeneral()
	{
		timer.start();
	}
}
